using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GameAOT;
using System.Collections.Generic;

namespace IQIGame.Onigao.GamePlay
{
    public class UI_FriendSearchMould : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UI_FriendSearchModule
        {
            #region ObjectBinding Generate
            public IQIGame.Onigao.Framework.ExText name { protected set; get; }
            public IQIGame.Onigao.Framework.ExText online { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnApplicate { protected set; get; }
            public UnityEngine.GameObject commonPlayerItem { protected set; get; }
            public UnityEngine.RectTransform playerCoatOfArms { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("name", out var __tbv0);
                this.name = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("online", out var __tbv1);
                this.online = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnApplicate", out var __tbv2);
                this.btnApplicate = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("commonPlayerItem", out var __tbv3);
                this.commonPlayerItem = __tbv3;
                __binding.TryGetVariableValue<UnityEngine.RectTransform>("playerCoatOfArms", out var __tbv4);
                this.playerCoatOfArms = __tbv4;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields
        private UIN_CommonPlayerItem playerItem = null;


        #endregion fields

        #region properties

        protected UIB_UI_FriendSearchModule ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => _msgDispatcher as UIMsgDispatcher;
        public FriendData friendData { get; private set; }

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UI_FriendSearchModule();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            playerItem = InitChildNode<UIN_CommonPlayerItem>(this.ui.commonPlayerItem);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {
            this.ui.btnApplicate.AddClickEvent(() => OnClickApply().Forget());
        }



        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {

        }

        #region methonds
        private async UniTaskVoid OnClickApply()
        {
            var list = new List<long>();
            list.Add(friendData.id);
            var res = await FriendModule.NetCore.CS_applyFriends(list);
            if (res.IsFail) return;
            FriendModule.Instance.DeleteSearchListData(friendData.id);
        }

        public void Show(FriendData friendData)
        {
            this.friendData = friendData;
            this.ui.name.text = this.friendData.playerName;

            this.playerItem.SetSelf(this.friendData.avatar, this.friendData.avatarFrame, this.friendData.level);
            this.playerItem.Show();
            //称号预设方式
            /*
            var CoatOfArms = this.resLoader.LoadABAsset<GameObject>(TableCenter.playerCoatOfArms.Get(friendData.title).Prefab_fullPath);
            GameObject.Instantiate(CoatOfArms, this.ui.playerCoatOfArms);
            */
            //称号图片方式
            var img = this.ui.playerCoatOfArms.GetComponentInChildren<ExImage>();
            if (friendData.title != 0) SetImageSprite(img, TableCenter.playerCoatOfArms.Get(friendData.title).Prefab_fullPath);

            FriendModuleExtensions.SetOnline(this.ui.online, this.friendData);
        }


        #endregion methonds
    }
}